package com.example.administrator.yunniuusedcar.fragment.ShoppInfo_Fragment;

import android.annotation.SuppressLint;
import android.support.design.widget.TabLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.administrator.common.base.BaseFragment;
import com.example.administrator.yunniuusedcar.R;
import com.example.administrator.yunniuusedcar.adapter.TabListViewAdapter;
import com.example.administrator.yunniuusedcar.bean.brand.BrandCar;
import com.example.administrator.yunniuusedcar.bean.brand.Data;
import com.example.administrator.yunniuusedcar.model.mode_benn.IHttpService;
import com.example.administrator.yunniuusedcar.model.mode_benn.RanCarXiangQingBean;
import com.example.administrator.yunniuusedcar.view.NoScrollListView;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/8.
 *  综合排序  . 商品详情
 */

@SuppressLint("ValidFragment")
public class JiBenInfoFragment extends BaseFragment {
    private int resultID;
    private TextView tv_car_name;
    private TextView JiFenJia;
    private TextView HuiYuanJia;
    private TextView ShiChangJia;
    private TextView tv_NumPager;
    private ImageView car_logo;


    private BrandCar brandCar;

    private TabLayout tab;
    private NoScrollListView listview;
    private TabListViewAdapter recyclerViewAdapter;
    @SuppressLint("ValidFragment")
    public JiBenInfoFragment(int car_id) {
        super();
        resultID = car_id;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_jiben_info;
    }

    @Override
    public void initView() {
        recyclerViewAdapter = new TabListViewAdapter(getContext());
        initFindViewId();
        loadDatas();
    }

    private void initFindViewId() {
        tv_car_name = findView(R.id.tv_car_name);
        JiFenJia = findView(R.id.JiFenJia);
        HuiYuanJia = findView(R.id.HuiYuanJia);
        ShiChangJia = findView(R.id.tv_ShiChangJia);
        tv_NumPager = findView(R.id.tv_NumPager);
        car_logo = findView(R.id.car_logo);

        tab = findView(R.id.tab);
        listview = findView(R.id.listview);
    }



    @Override
    public void initListener() {
        tab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                recyclerViewAdapter.setData(brandCar.getData().get(position));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });



        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //点击item就换

                //获取tab选中页卡
                int tab_select = tab.getSelectedTabPosition();
                Data data = brandCar.getData().get(tab_select).getData().get(i);

                resultID = (int) data.getCar_id();

                tv_car_name.setText( data.getName());
                JiFenJia.setText("积分优惠价: ¥"+(data.getIntegral_range()==null?"0万":data.getIntegral_range()+"万"));
                HuiYuanJia.setText("会员价:¥"+(data.getMember_range()==null?"0万":data.getMember_range()+"万"));
                ShiChangJia.setText("市场价: ¥"+(data.getMarket_price()==null?"0万":data.getMarket_price()+"万"));
                //tv_NumPager.setText(data.get+"张");
                //Picasso.with(getContext()).load(data.getLo).error(R.mipmap.error).into(car_logo);
            }
        });
    }

    @Override
    public void initData() {

    }

    private void loadDatas() {

        String ul = IHttpService.URL2 + IHttpService.GETCARDETAIL;    // 商品 详情
        //String.valueOf(resultID)  car_id
        OkHttpUtils.post().url(ul).addParams("sign",IHttpService.SignVal).addParams("car_id", String.valueOf(resultID)).build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        RanCarXiangQingBean ranCarInfoBean = new Gson().fromJson(response, RanCarXiangQingBean.class);
                        if(ranCarInfoBean.code==1){
                            tv_car_name.setText( ranCarInfoBean.data.list.name);
                            JiFenJia.setText("积分优惠价: ¥"+(ranCarInfoBean.data.list.integral_range==null?"0万":ranCarInfoBean.data.list.integral_range+"万"));
                            HuiYuanJia.setText("会员价:¥"+(ranCarInfoBean.data.list.member_range==null?"0万":ranCarInfoBean.data.list.member_range+"万"));
                            ShiChangJia.setText("市场价: ¥"+(ranCarInfoBean.data.list.market_price==null?"0万":ranCarInfoBean.data.list.market_price+"万"));
                            tv_NumPager.setText(ranCarInfoBean.data.list.num+"张");
                            Picasso.with(getContext()).load(ranCarInfoBean.data.list.logo).error(R.mipmap.error).into(car_logo);
                        }
                    }
                });



        //获取同品牌的车
        OkHttpUtils.post().url(IHttpService.URL2 + IHttpService.GETBRANDCAR).addParams("sign",IHttpService.SignVal).addParams("car_id", String.valueOf(resultID)).build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        brandCar = gson.fromJson(response, BrandCar.class);
                        tab.setTabMode(TabLayout.MODE_SCROLLABLE);
                        for(int i=0;i<brandCar.getData().size();i++){
                            TabLayout.Tab tab1 = tab.newTab().setText(brandCar.getData().get(i).getYear()+"");
                            tab.addTab(tab1);
                        }
                        listview.setAdapter(recyclerViewAdapter);
                    }
                });
    }
}
